Skip to main content

Chat Consultation

ChatConfig

For chat consultations, the consultation data will include the ChatConfig object This config data will be used to initiate and handle the chat between the user and the doctor

class ChatConfig {
int? id;
int? consultationId;
String? groupId;
String? chatUserId;
String? appId;
String? chatUserToken;

ChatConfig({
this.id,
this.consultationId,
this.groupId,
this.appId,
this.chatUserId,
this.chatUserToken,
});
}

Initiating the chat service

First the chat service should be initiated using the AltibbiChat.init() method with the Consultation from the getConsultationInfo

AltibbiChat().init(consultation: consultation);

Adding Event Listeners and Handlers

You have to add some handler functions to the chatService channel, and there are one types of handlers GroupChannelHandler.

Check the code below to see how the handlers should be declared and added to the chatService channel

GroupChannelHandlers

class MyChannelHandler extends GroupChannelHandler {
final Function(BaseMessage) onChannelMessageReceived;
final Consultation consultation;

MyChannelHandler({required this.consultation, required this.onChannelMessageReceived});
@override
Future<void> onMessageReceived(BaseChannel channel, BaseMessage message) async {
if(message.message.isNotEmpty){
onChannelMessageReceived(message);
}
}
@override
Future<void> onTypingStatusUpdated(BaseChannel channel) async {
print("typing started from Dr side");
}

@override
Future<void> onUserLeft(BaseChannel channel, User user) async {
print("Chat finished");
}
}

MyChannelHandler channelHandler = MyChannelHandler(
onChannelMessageReceived: (BaseMessage message) {
setState(() {
messages.add(message);
_scrollToBottom();
});
},
consultation: consultation
);
AltibbiChat().addChannelHandler('myChannelHandler', channelHandler);

Loading All Channel Messages

After initiate the AltibbiChat, you have to load the previous messages of the channel

From the ChatConfig data, the groupId represents the channelURL which is needed to load the previous messages Check the code below:

PreviousMessageListQuery previousMessageListQuery = PreviousMessageListQuery(channelType: ChannelType.group,
channelUrl: "channel_${consultation.chatConfig!.groupId!}");
previousMessageListQuery.limit = 200;
var messages = await previousMessageListQuery.next();
while (messages.isNotEmpty) {
messages.addAll(messages);
messages = await previousMessageListQuery.next();
}

Sending Messages

Now the AltibbiChat is initiated, connected and started. So you can start sending messages using sendUserMessage from the AltibbiChat with the message object that includes a message attribute

Check the code below:

const message = "Message example"

GroupChannel groupChannels = await AltibbiChat().getGroupChannel(consultation);
groupChannels.sendUserMessage(UserMessageCreateParams(message: message));

Sending media files (Pictures or PDFs)

You can send pictures and PDF files on the chat using the previously mentioned uploadMedia functionality

The process could be done using the uploadMedia and getting the url from the response and send it just as a text

Send User Typing

You can register an event to indicate typing status by using startTyping() and endTyping()

GroupChannel groupChannels = await AltibbiChat().getGroupChannel(consultation);

// When the user start typing in the TextInput
groupChannels.startTyping();
// When the user stop typing
groupChannels.endTyping()